package postwp.controller;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import data.Data;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
		} catch (Exception localException) {
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		DBCollection coll = Data.data("users");
		String username = req.getParameter("username");
		String password = req.getParameter("password");

		DBObject obj = coll.findOne(new BasicDBObject("user_login", username)
				.append("pwd_login", password));
		if (obj != null) {
			HttpSession session = req.getSession();
			session.setAttribute("users", obj);
			resp.sendRedirect("/home");
		} else {
			resp.sendRedirect("/login");
		}
	}
}